package MyUtil;


import javax.swing.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * CSV文件导出工具类
 *
 */
public class CSVUtil {
    private CSVUtil(){
    }
    public static void searchSavePath(JFrame parent,List<Object[]> list,String[] headString,String initalValue){
        JFileChooser chooser = new JFileChooser();
        // 设置模式：仅选择目录
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        // 显示对话框
        int ret = chooser.showSaveDialog(parent);
        if (ret == JFileChooser.APPROVE_OPTION)
        {
            // 结果为： 已经存在的一个目录
            File dir = chooser.getSelectedFile();
            String input = JOptionPane.showInputDialog(
                    parent,
                    "请输入新建文件名称",
                    initalValue);
            CSVUtil.createCSVFile(headString,list,dir.getAbsolutePath(),input);
        }
    }
    public static File createCSVFile(Object[] head, List<Object[]> lsNew,String outPutPath, String filename) {

        File csvFile = null;
        BufferedWriter csvWriter = null;
        try {
            csvFile = new File(outPutPath + File.separator + filename + ".csv");
            csvFile.createNewFile();

            // GB2312使正确读取分隔符","
            csvWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "GB2312"), 1024);
            // 写入文件头部
            writeRow(head, csvWriter);

            // 写入文件内容
            if (lsNew != null && lsNew.size() != 0){
                for (Object[] row : lsNew) {
                    writeRow(row, csvWriter);
                }
                csvWriter.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                csvWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return csvFile;
    }
    /**
     * 写一行数据方法
     * @param row
     * @param csvWriter
     * @throws IOException
     */
    private static void writeRow(Object[] row, BufferedWriter csvWriter) throws IOException {
        // 写入文件头部
        for (Object data : row) {
            StringBuffer sb = new StringBuffer();
            String rowStr = sb.append("\"").append(data).append("\",").toString();
            csvWriter.write(rowStr);
        }
        csvWriter.newLine();
    }

    /**
     * 导入
     *
     * @param file csv文件(路径+文件)
     * @return
     */
    public static List<String> importCsv(File file){
        List<String> dataList=new ArrayList<String>();
        BufferedReader br=null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line = "";
            while ((line = br.readLine()) != null) {
                dataList.add(line);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(br!=null){
                try {
                    br.close();
                    br=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return dataList;
    }

}
